<html>
    <head>
        <link href="style3.css" rel="stylesheet" type="text/css"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <script type="text/javascript">
            //a function that takes the action and changes the status of the submit button from
            //disabled to enabled or vice versa
            function change(e)
            {
                document.form.send_button.disabled=true;
                if(e.checked==true)
                {
                    document.form.send_button.disabled=false;
                }
                if(e.checked==false)
                {
                    document.form.send_button.enabled=false;
                }
            }
        </script>

        <title>Assign Reviewer</title></head>
    <body>
        <form name= "form" action = "" value = "Submit" method = "Post">

            <?php
            (include "functions.php");
            //check if the values of the paperID, the conferenceID and the paper title were passed from the previous page
            if (isset($_GET['paper_id']) & isset($_GET['conference_id'])) {
                $paperID = intval($_GET['paper_id']); //store the value of the paperID from previous page
                $confID = intval($_GET['conference_id']); //store the value of the conferenceID from the previous page
                //calling function assignReviewer to execute it if conditions were satisfied
                assignReviewer($paperID, $confID);
            } else {//if the user for any reason was redirected to the assignReviewer page without having the paperId, confId and paper title
                echo "Sorry, action cannot be performed!";
            }

            Function assignReviewer($paperID, $confID) {

                $conn = mysql_connect("localhost", "root", ""); //connect to the database

                if (!$conn) {//if no connection was made
                    die('Could not connect:' . mysql_error());
                }

                mysql_select_db("mydb", $conn); //select database
                //get the conference long name and display it on the top of the page
                $conference_name = get_conference_name($confID);


                print "<h1>{$conference_name}</h1>";

                //get the paper title and display it under the conf name

                $paper_title = get_paper_title($paperID);
                print "<h2>$paper_title</h2>";



                if (!isset($_POST['send_button'])) { //if the submit button was not yet clicked
                    //a query that selects the first name, last name and area of expertise of the reviewers of the current conference who havent exceeded the limit of papers they can review
                    $view_reviewers = mysql_query("SELECT M.first_name, M.last_name, AOE.area_of_expertise_text, M.member_id
                                           FROM Member M INNER JOIN member_request_member R
                                           ON M.member_id = R.member_id2
                                           INNER JOIN member_privileges MP
                                           ON M.member_id = MP.member_id
                                           INNER JOIN privileges Pr
                                           ON MP.privileges_id = Pr.privileges_id
                                           INNER JOIN member_expert_conf MEC
                                           ON MP.member_id = MEC.member_id
                                           INNER JOIN area_of_expertise AOE
                                           ON AOE.area_of_expertise_id = MEC.area_of_expertise_id
                                           INNER JOIN limit_paper LP
                                           ON MEC.member_id = LP.member_id
                                           INNER JOIN paper P
                                           ON P.category_id = LP.category_id
                                           WHERE MP.conference_id = '$confID'
                                           AND P.paper_id = '$paperID'
                                           AND R.approve = 1
                                           AND R.type = 'R'
                                           AND Pr.role = 'reviewer'
                                           AND M.member_id NOT IN (SELECT AP.member_id
                                                                   FROM assigned_papers AP
                                                                   WHERE AP.paper_id = '$paperID'
                                                                   AND AP.conference_id = '$confID')
                                           AND LP.maximum_papers - LP.assigned_papers >= 1;")
                            or die(mysql_error());
                    //if there are no remaining reviewers to assign papers to in the db
                    if (!mysql_num_rows($view_reviewers)) {
                        echo '<p>There are no reviewers to assign this paper to!</p>';
                    } else if ($view_reviewers) {

                        print "<br>
                    <p> Assign Reviewers:</p>
                    </br>";

                        //create table that displays the names and areas of expertise of all the reviewers in this conference that have not exceeded the maximum limit of papers and that are not already assigned to this paper

                        print "<table width=500 border=1>\n";
                        print "<tr>\n";

                        print "<th>&nbsp; </th>\n";
                        print "<th><width=\"0%\" height=\"0\" style=\"display:none\" value=\"Member ID\"</th>\n";
                        print "<th> Reviewer Name </th>\n";
                        print "<th> Area of Expertise </th>\n";

                        print "</tr>\n";

                        $counter = 0;
                        //fetching member id (to be the value of the checkbox),first name, last name and area of expertise from database
                        while ($row = mysql_fetch_array($view_reviewers)) {
                            $counter++;
                            print "<tr>\n";
                            print "<td><input type=\"checkbox\" name=\"reviewers$counter\" value=\"$row[member_id]\" onClick=\"change(this)\"></td>\n";
                            echo "<td><width=\"0%\" height=\"0\" style=\"display:none\" value=\"{$row['member_id']}\"</td>\n";
                            echo "<td>{$row['first_name']} {$row['last_name']}</td>\n";
                            echo "<td>{$row['area_of_expertise_text']}</td>\n";

                            echo "</tr>\n";
                        }

                        // print the table
                        print "</table>\n";

                        //a hidden input type to store the variable $counter but keep it invisible from the user
                        print "<input type=\"hidden\" name=\"reviewers\" value=\"$counter\"/>\n";
                        //print the submit button that will be used later to execute $assign_paper and $update _assigned_papers, it is disabled until a checkbox or more is clicked
                        print "<input type=\"submit\" name=\"send_button\" value=\"Assign Reviewer(s)\" id=\"toEnable\" disabled=\"disabled\"/>\n";
                    }
                } else { //else if submit button was clicked
                    for ($counter = 1; $counter <= $_POST['reviewers']; $counter++) {
//loop on the array of checkboxes (important because more than 1 checkbox may be selected)

                        if (isset($_POST["reviewers$counter"])) { //make sure the box was checked
                            $memberID = $_POST["reviewers$counter"]; //assign the value of the box checked to a variable $var
//execute query that assigns a paper to one or more reviewer by adding a row in table assigned_papers

                            $assign_paper = mysql_query("INSERT INTO assigned_papers (member_id, conference_id, paper_id)
                                                    VALUES ('$memberID', '$confID', '$paperID')")
                                    or die(mysql_error());
//the query updates the assigned papers of the reviewer to become assigned papers + 1

                            $update_assigned_papers = mysql_query("UPDATE limit_paper
                                                    SET assigned_papers = assigned_papers + 1
                                                    WHERE member_id = '$memberID'
                                                    AND maximum_papers - assigned_papers >=1
                                                    AND conference_id = '$confID'
                                                    AND category_id IN (SELECT DISTINCT C.category_id
                                                                        FROM category C INNER JOIN paper P
                                                                        ON C.category_id = P.category_id
                                                                        WHERE C.category_id = P.category_id
                                                                        AND P.paper_id = '$paperID')") or die(mysql_error());
                        }
                    }//end for loop
                    //if both queries were executed successfuly
                    if ($assign_paper && $update_assigned_papers) {
                        echo "Reviewer(s) assigned!";
                        //if there was an error in executing queries
                    } else {

                        echo ("<p>There was an error in executing the query: " .
                        mysql_error() . "</p>");
                    }

                    //close connection
                    mysql_close($conn);
                }
            }
            ?>

        </form>
    </body>
</html>